/*     */ package com.zimbra.cs.datasource;
/*     */ 
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.DateUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.TaskUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.DataSource;
/*     */ import com.zimbra.cs.account.DataSource.DataImport;
/*     */ import com.zimbra.cs.account.DataSourceConfig;
/*     */ import com.zimbra.cs.account.DataSourceConfig.Service;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.datasource.imap.ImapSync;
/*     */ import com.zimbra.cs.db.DbPool;
/*     */ import com.zimbra.cs.db.DbPool.DbConnection;
/*     */ import com.zimbra.cs.db.DbScheduledTask;
/*     */ import com.zimbra.cs.extension.ExtensionUtil;
/*     */ import com.zimbra.cs.gal.GalImport;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.ScheduledTaskManager;
/*     */ import com.zimbra.cs.util.Zimbra;
/*     */ import com.zimbra.soap.admin.type.DataSourceType;
/*     */ import java.io.File;
/*     */ import java.lang.reflect.Constructor;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collections;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import java.util.concurrent.ConcurrentHashMap;
/*     */ import java.util.concurrent.ExecutorService;
/*     */ import java.util.concurrent.Executors;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DataSourceManager
/*     */ {
/*     */   private static DataSourceManager sInstance;
/*  59 */   private static final Map<String, Map<String, ImportStatus>> sImportStatus = new HashMap();
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  65 */   private static final Set<Object> sManagedDataSources = newConcurrentHashSet();
/*     */   
/*     */   private final DataSourceConfig config;
/*     */   
/*  69 */   private static final ExecutorService executor = Executors.newCachedThreadPool(TaskUtil.newDaemonThreadFactory("ImportData"));
/*     */   
/*     */   private static <E> Set<E> newConcurrentHashSet() {
/*  72 */     return Collections.newSetFromMap(new ConcurrentHashMap());
/*     */   }
/*     */   
/*     */   private static Object key(String accountId, String dataSourceId) {
/*  76 */     return new Pair(accountId, dataSourceId);
/*     */   }
/*     */   
/*     */   public static void addManaged(DataSource ds) {
/*  80 */     sManagedDataSources.add(key(ds.getAccountId(), ds.getId()));
/*     */   }
/*     */   
/*     */   public static void deleteManaged(String accountId, String dataSourceId) {
/*  84 */     sManagedDataSources.remove(key(accountId, dataSourceId));
/*     */   }
/*     */   
/*     */   public static boolean isManaged(DataSource ds) {
/*  88 */     return sManagedDataSources.contains(key(ds.getAccountId(), ds.getId()));
/*     */   }
/*     */   
/*     */   public DataSourceManager() {
/*  92 */     this.config = loadConfig();
/*     */   }
/*     */   
/*     */   private DataSourceConfig loadConfig() {
/*     */     try {
/*  97 */       File file = new File(LC.data_source_config.value());
/*  98 */       DataSourceConfig config = DataSourceConfig.read(file);
/*  99 */       ZimbraLog.datasource.debug("Loaded datasource configuration from '%s'", new Object[] { file });
/*     */       
/* 101 */       for (DataSourceConfig.Service service : config.getServices()) {
/* 102 */         ZimbraLog.datasource.debug("Loaded %d folder mappings for service '%s'", new Object[] { Integer.valueOf(service.getFolders().size()), service.getName() });
/*     */       }
/*     */       
/*     */ 
/* 106 */       return config;
/*     */     }
/*     */     catch (Exception e) {
/* 109 */       Zimbra.halt("Unable to load datasource config", e); }
/* 110 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public boolean isSyncCapable(DataSource ds, Folder folder)
/*     */   {
/* 119 */     return true;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public boolean isSyncEnabled(DataSource ds, Folder folder)
/*     */   {
/* 127 */     return true;
/*     */   }
/*     */   
/*     */   public static synchronized DataSourceManager getInstance() {
/* 131 */     if (sInstance == null) {
/* 132 */       String className = LC.zimbra_class_datasourcemanager.value();
/* 133 */       if (!StringUtil.isNullOrEmpty(className)) {
/*     */         try {
/*     */           try {
/* 136 */             sInstance = (DataSourceManager)Class.forName(className).newInstance();
/*     */           }
/*     */           catch (ClassNotFoundException cnfe) {
/* 139 */             sInstance = (DataSourceManager)ExtensionUtil.findClass(className).newInstance();
/*     */           }
/*     */         } catch (Exception e) {
/* 142 */           ZimbraLog.system.error("Unable to initialize %s.", className, e);
/*     */         }
/*     */       }
/* 145 */       if (sInstance == null) {
/* 146 */         sInstance = new DataSourceManager();
/* 147 */         ZimbraLog.datasource.info("Initialized %s.", new Object[] { sInstance.getClass().getName() });
/*     */       }
/*     */     }
/*     */     
/* 151 */     return sInstance;
/*     */   }
/*     */   
/*     */   public static DataSourceConfig getConfig() {
/* 155 */     return getInstance().config;
/*     */   }
/*     */   
/*     */   public Mailbox getMailbox(DataSource ds) throws ServiceException {
/* 159 */     return MailboxManager.getInstance().getMailboxByAccount(ds.getAccount());
/*     */   }
/*     */   
/*     */   public DataSource.DataImport getDataImport(DataSource ds) throws ServiceException {
/* 163 */     switch (ds.getType()) {
/*     */     case pop3: 
/* 165 */       return new Pop3Sync(ds);
/*     */     case imap: 
/* 167 */       return new ImapSync(ds);
/*     */     case caldav: 
/* 169 */       return new CalDavDataImport(ds);
/*     */     case rss: 
/*     */     case cal: 
/* 172 */       return new RssImport(ds);
/*     */     case gal: 
/* 174 */       return new GalImport(ds);
/*     */     case xsync: 
/*     */       try {
/* 177 */         String className = LC.data_source_xsync_class.value();
/* 178 */         if ((className != null) && (className.length() > 0)) {
/* 179 */           Class<?> cmdClass = null;
/*     */           try {
/* 181 */             cmdClass = Class.forName(className);
/*     */           } catch (ClassNotFoundException x) {
/* 183 */             cmdClass = ExtensionUtil.findClass(className);
/*     */           }
/* 185 */           Constructor<?> constructor = cmdClass.getConstructor(new Class[] { DataSource.class });
/* 186 */           return (DataSource.DataImport)constructor.newInstance(new Object[] { ds });
/*     */         }
/*     */       } catch (Exception x) {
/* 189 */         ZimbraLog.datasource.warn("Failed instantiating xsync class: %s", ds, x);
/*     */       }
/*     */     }
/*     */     
/* 193 */     throw new IllegalArgumentException("Unknown data import type: " + ds.getType());
/*     */   }
/*     */   
/*     */   public static String getDefaultImportClass(DataSourceType ds)
/*     */   {
/* 198 */     switch (ds) {
/*     */     case caldav: 
/* 200 */       return CalDavDataImport.class.getName();
/*     */     case gal: 
/* 202 */       return GalImport.class.getName();
/*     */     }
/* 204 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static void test(DataSource ds)
/*     */     throws ServiceException
/*     */   {
/* 212 */     ZimbraLog.datasource.info("Testing: %s", new Object[] { ds });
/*     */     try {
/* 214 */       DataSource.DataImport di = getInstance().getDataImport(ds);
/* 215 */       di.test();
/* 216 */       ZimbraLog.datasource.info("Test succeeded: %s", new Object[] { ds });
/*     */     } catch (ServiceException x) {
/* 218 */       ZimbraLog.datasource.warn("Test failed: %s", ds, x);
/* 219 */       throw x;
/*     */     }
/*     */   }
/*     */   
/*     */   public static List<ImportStatus> getImportStatus(Account account) throws ServiceException
/*     */   {
/* 225 */     List<DataSource> dsList = Provisioning.getInstance().getAllDataSources(account);
/* 226 */     List<ImportStatus> allStatus = new ArrayList();
/* 227 */     for (DataSource ds : dsList) {
/* 228 */       allStatus.add(getImportStatus(account, ds));
/*     */     }
/* 230 */     return allStatus;
/*     */   }
/*     */   
/*     */   public static ImportStatus getImportStatus(Account account, DataSource ds)
/*     */   {
/*     */     ImportStatus importStatus;
/* 236 */     synchronized (sImportStatus) {
/* 237 */       Map<String, ImportStatus> isMap = (Map)sImportStatus.get(account.getId());
/* 238 */       if (isMap == null) {
/* 239 */         isMap = new HashMap();
/* 240 */         sImportStatus.put(account.getId(), isMap);
/*     */       }
/* 242 */       importStatus = (ImportStatus)isMap.get(ds.getId());
/* 243 */       if (importStatus == null) {
/* 244 */         importStatus = new ImportStatus(ds.getId());
/* 245 */         isMap.put(ds.getId(), importStatus);
/*     */       }
/*     */     }
/*     */     
/* 249 */     return importStatus;
/*     */   }
/*     */   
/*     */   public static void asyncImportData(DataSource ds) {
/* 253 */     ZimbraLog.datasource.debug("Requesting async import for DataSource %s", new Object[] { ds.getId() });
/*     */     
/* 255 */     executor.submit(new Runnable()
/*     */     {
/*     */       public void run()
/*     */       {
/*     */         try {
/* 260 */           ZimbraLog.clearContext();
/* 261 */           ZimbraLog.addMboxToContext(this.val$ds.getMailbox().getId());
/* 262 */           ZimbraLog.addAccountNameToContext(this.val$ds.getAccount().getName());
/* 263 */           ZimbraLog.addDataSourceNameToContext(this.val$ds.getName());
/* 264 */           ZimbraLog.datasource.debug("Running on-demand import for DataSource %s", new Object[] { this.val$ds.getId() });
/*     */           
/* 266 */           DataSourceManager.importData(this.val$ds);
/*     */         }
/*     */         catch (Exception e) {
/* 269 */           ZimbraLog.datasource.warn("On-demand DataSource import failed.", e);
/*     */         }
/*     */         finally {
/* 272 */           ZimbraLog.clearContext();
/*     */         }
/*     */       }
/*     */     });
/*     */   }
/*     */   
/*     */   public static void importData(DataSource ds) throws ServiceException {
/* 279 */     importData(ds, null, true);
/*     */   }
/*     */   
/*     */   public static void importData(DataSource fs, boolean fullSync) throws ServiceException
/*     */   {
/* 284 */     importData(fs, null, fullSync);
/*     */   }
/*     */   
/*     */   /* Error */
/*     */   public static void importData(DataSource ds, List<Integer> folderIds, boolean fullSync)
/*     */     throws ServiceException
/*     */   {
/*     */     // Byte code:
/*     */     //   0: getstatic 21	com/zimbra/common/util/ZimbraLog:datasource	Lcom/zimbra/common/util/Log;
/*     */     //   3: ldc 115
/*     */     //   5: invokevirtual 116	com/zimbra/common/util/Log:info	(Ljava/lang/Object;)V
/*     */     //   8: aload_0
/*     */     //   9: invokevirtual 56	com/zimbra/cs/account/DataSource:getAccount	()Lcom/zimbra/cs/account/Account;
/*     */     //   12: invokevirtual 117	com/zimbra/cs/account/Account:getAccountStatus	()Lcom/zimbra/common/account/ZAttrProvisioning$AccountStatus;
/*     */     //   15: astore_3
/*     */     //   16: aload_3
/*     */     //   17: invokevirtual 118	com/zimbra/common/account/ZAttrProvisioning$AccountStatus:isActive	()Z
/*     */     //   20: ifne +26 -> 46
/*     */     //   23: aload_3
/*     */     //   24: invokevirtual 119	com/zimbra/common/account/ZAttrProvisioning$AccountStatus:isLocked	()Z
/*     */     //   27: ifne +19 -> 46
/*     */     //   30: aload_3
/*     */     //   31: invokevirtual 120	com/zimbra/common/account/ZAttrProvisioning$AccountStatus:isLockout	()Z
/*     */     //   34: ifne +12 -> 46
/*     */     //   37: getstatic 21	com/zimbra/common/util/ZimbraLog:datasource	Lcom/zimbra/common/util/Log;
/*     */     //   40: ldc 121
/*     */     //   42: invokevirtual 116	com/zimbra/common/util/Log:info	(Ljava/lang/Object;)V
/*     */     //   45: return
/*     */     //   46: invokestatic 54	com/zimbra/cs/datasource/DataSourceManager:getInstance	()Lcom/zimbra/cs/datasource/DataSourceManager;
/*     */     //   49: aload_0
/*     */     //   50: invokevirtual 122	com/zimbra/cs/datasource/DataSourceManager:getMailbox	(Lcom/zimbra/cs/account/DataSource;)Lcom/zimbra/cs/mailbox/Mailbox;
/*     */     //   53: invokevirtual 123	com/zimbra/cs/mailbox/Mailbox:getMaintenance	()Lcom/zimbra/cs/mailbox/MailboxMaintenance;
/*     */     //   56: ifnull +12 -> 68
/*     */     //   59: getstatic 21	com/zimbra/common/util/ZimbraLog:datasource	Lcom/zimbra/common/util/Log;
/*     */     //   62: ldc 124
/*     */     //   64: invokevirtual 116	com/zimbra/common/util/Log:info	(Ljava/lang/Object;)V
/*     */     //   67: return
/*     */     //   68: aload_0
/*     */     //   69: invokevirtual 56	com/zimbra/cs/account/DataSource:getAccount	()Lcom/zimbra/cs/account/Account;
/*     */     //   72: aload_0
/*     */     //   73: invokestatic 98	com/zimbra/cs/datasource/DataSourceManager:getImportStatus	(Lcom/zimbra/cs/account/Account;Lcom/zimbra/cs/account/DataSource;)Lcom/zimbra/cs/datasource/ImportStatus;
/*     */     //   76: astore 4
/*     */     //   78: aload 4
/*     */     //   80: dup
/*     */     //   81: astore 5
/*     */     //   83: monitorenter
/*     */     //   84: aload 4
/*     */     //   86: invokevirtual 125	com/zimbra/cs/datasource/ImportStatus:isRunning	()Z
/*     */     //   89: ifeq +15 -> 104
/*     */     //   92: getstatic 21	com/zimbra/common/util/ZimbraLog:datasource	Lcom/zimbra/common/util/Log;
/*     */     //   95: ldc 126
/*     */     //   97: invokevirtual 116	com/zimbra/common/util/Log:info	(Ljava/lang/Object;)V
/*     */     //   100: aload 5
/*     */     //   102: monitorexit
/*     */     //   103: return
/*     */     //   104: aload 4
/*     */     //   106: iconst_1
/*     */     //   107: putfield 127	com/zimbra/cs/datasource/ImportStatus:mHasRun	Z
/*     */     //   110: aload 4
/*     */     //   112: iconst_1
/*     */     //   113: putfield 128	com/zimbra/cs/datasource/ImportStatus:mIsRunning	Z
/*     */     //   116: aload 4
/*     */     //   118: iconst_0
/*     */     //   119: putfield 129	com/zimbra/cs/datasource/ImportStatus:mSuccess	Z
/*     */     //   122: aload 4
/*     */     //   124: aconst_null
/*     */     //   125: putfield 130	com/zimbra/cs/datasource/ImportStatus:mError	Ljava/lang/String;
/*     */     //   128: aload 5
/*     */     //   130: monitorexit
/*     */     //   131: goto +11 -> 142
/*     */     //   134: astore 6
/*     */     //   136: aload 5
/*     */     //   138: monitorexit
/*     */     //   139: aload 6
/*     */     //   141: athrow
/*     */     //   142: iconst_0
/*     */     //   143: istore 5
/*     */     //   145: aconst_null
/*     */     //   146: astore 6
/*     */     //   148: aload_0
/*     */     //   149: invokestatic 131	com/zimbra/cs/datasource/DataSourceManager:addManaged	(Lcom/zimbra/cs/account/DataSource;)V
/*     */     //   152: getstatic 21	com/zimbra/common/util/ZimbraLog:datasource	Lcom/zimbra/common/util/Log;
/*     */     //   155: ldc -124
/*     */     //   157: iconst_1
/*     */     //   158: anewarray 23	java/lang/Object
/*     */     //   161: dup
/*     */     //   162: iconst_0
/*     */     //   163: aload_0
/*     */     //   164: invokevirtual 133	com/zimbra/cs/account/DataSource:getName	()Ljava/lang/String;
/*     */     //   167: aastore
/*     */     //   168: invokevirtual 53	com/zimbra/common/util/Log:info	(Ljava/lang/String;[Ljava/lang/Object;)V
/*     */     //   171: invokestatic 54	com/zimbra/cs/datasource/DataSourceManager:getInstance	()Lcom/zimbra/cs/datasource/DataSourceManager;
/*     */     //   174: aload_0
/*     */     //   175: invokevirtual 89	com/zimbra/cs/datasource/DataSourceManager:getDataImport	(Lcom/zimbra/cs/account/DataSource;)Lcom/zimbra/cs/account/DataSource$DataImport;
/*     */     //   178: aload_1
/*     */     //   179: iload_2
/*     */     //   180: invokeinterface 134 3 0
/*     */     //   185: iconst_1
/*     */     //   186: istore 5
/*     */     //   188: aload_0
/*     */     //   189: invokestatic 135	com/zimbra/cs/datasource/DataSourceManager:resetErrorStatus	(Lcom/zimbra/cs/account/DataSource;)V
/*     */     //   192: getstatic 21	com/zimbra/common/util/ZimbraLog:datasource	Lcom/zimbra/common/util/Log;
/*     */     //   195: ldc -120
/*     */     //   197: iconst_1
/*     */     //   198: anewarray 23	java/lang/Object
/*     */     //   201: dup
/*     */     //   202: iconst_0
/*     */     //   203: aload_0
/*     */     //   204: invokevirtual 133	com/zimbra/cs/account/DataSource:getName	()Ljava/lang/String;
/*     */     //   207: aastore
/*     */     //   208: invokevirtual 53	com/zimbra/common/util/Log:info	(Ljava/lang/String;[Ljava/lang/Object;)V
/*     */     //   211: aload 4
/*     */     //   213: dup
/*     */     //   214: astore 7
/*     */     //   216: monitorenter
/*     */     //   217: aload 4
/*     */     //   219: iload 5
/*     */     //   221: putfield 129	com/zimbra/cs/datasource/ImportStatus:mSuccess	Z
/*     */     //   224: aload 4
/*     */     //   226: aload 6
/*     */     //   228: putfield 130	com/zimbra/cs/datasource/ImportStatus:mError	Ljava/lang/String;
/*     */     //   231: aload 4
/*     */     //   233: iconst_0
/*     */     //   234: putfield 128	com/zimbra/cs/datasource/ImportStatus:mIsRunning	Z
/*     */     //   237: aload 7
/*     */     //   239: monitorexit
/*     */     //   240: goto +11 -> 251
/*     */     //   243: astore 8
/*     */     //   245: aload 7
/*     */     //   247: monitorexit
/*     */     //   248: aload 8
/*     */     //   250: athrow
/*     */     //   251: goto +85 -> 336
/*     */     //   254: astore 7
/*     */     //   256: aload 7
/*     */     //   258: invokestatic 137	com/zimbra/cs/datasource/DataSourceManager:generateErrorMessage	(Ljava/lang/Throwable;)Ljava/lang/String;
/*     */     //   261: astore 6
/*     */     //   263: aload_0
/*     */     //   264: aload 6
/*     */     //   266: invokestatic 138	com/zimbra/cs/datasource/DataSourceManager:setErrorStatus	(Lcom/zimbra/cs/account/DataSource;Ljava/lang/String;)V
/*     */     //   269: aload 7
/*     */     //   271: athrow
/*     */     //   272: astore 9
/*     */     //   274: getstatic 21	com/zimbra/common/util/ZimbraLog:datasource	Lcom/zimbra/common/util/Log;
/*     */     //   277: ldc -120
/*     */     //   279: iconst_1
/*     */     //   280: anewarray 23	java/lang/Object
/*     */     //   283: dup
/*     */     //   284: iconst_0
/*     */     //   285: aload_0
/*     */     //   286: invokevirtual 133	com/zimbra/cs/account/DataSource:getName	()Ljava/lang/String;
/*     */     //   289: aastore
/*     */     //   290: invokevirtual 53	com/zimbra/common/util/Log:info	(Ljava/lang/String;[Ljava/lang/Object;)V
/*     */     //   293: aload 4
/*     */     //   295: dup
/*     */     //   296: astore 10
/*     */     //   298: monitorenter
/*     */     //   299: aload 4
/*     */     //   301: iload 5
/*     */     //   303: putfield 129	com/zimbra/cs/datasource/ImportStatus:mSuccess	Z
/*     */     //   306: aload 4
/*     */     //   308: aload 6
/*     */     //   310: putfield 130	com/zimbra/cs/datasource/ImportStatus:mError	Ljava/lang/String;
/*     */     //   313: aload 4
/*     */     //   315: iconst_0
/*     */     //   316: putfield 128	com/zimbra/cs/datasource/ImportStatus:mIsRunning	Z
/*     */     //   319: aload 10
/*     */     //   321: monitorexit
/*     */     //   322: goto +11 -> 333
/*     */     //   325: astore 11
/*     */     //   327: aload 10
/*     */     //   329: monitorexit
/*     */     //   330: aload 11
/*     */     //   332: athrow
/*     */     //   333: aload 9
/*     */     //   335: athrow
/*     */     //   336: return
/*     */     // Line number table:
/*     */     //   Java source line #293	-> byte code offset #0
/*     */     //   Java source line #294	-> byte code offset #8
/*     */     //   Java source line #295	-> byte code offset #16
/*     */     //   Java source line #296	-> byte code offset #37
/*     */     //   Java source line #297	-> byte code offset #45
/*     */     //   Java source line #299	-> byte code offset #46
/*     */     //   Java source line #300	-> byte code offset #59
/*     */     //   Java source line #301	-> byte code offset #67
/*     */     //   Java source line #303	-> byte code offset #68
/*     */     //   Java source line #304	-> byte code offset #78
/*     */     //   Java source line #305	-> byte code offset #84
/*     */     //   Java source line #306	-> byte code offset #92
/*     */     //   Java source line #308	-> byte code offset #100
/*     */     //   Java source line #310	-> byte code offset #104
/*     */     //   Java source line #311	-> byte code offset #110
/*     */     //   Java source line #312	-> byte code offset #116
/*     */     //   Java source line #313	-> byte code offset #122
/*     */     //   Java source line #314	-> byte code offset #128
/*     */     //   Java source line #316	-> byte code offset #142
/*     */     //   Java source line #317	-> byte code offset #145
/*     */     //   Java source line #319	-> byte code offset #148
/*     */     //   Java source line #322	-> byte code offset #152
/*     */     //   Java source line #323	-> byte code offset #171
/*     */     //   Java source line #324	-> byte code offset #185
/*     */     //   Java source line #325	-> byte code offset #188
/*     */     //   Java source line #331	-> byte code offset #192
/*     */     //   Java source line #332	-> byte code offset #211
/*     */     //   Java source line #333	-> byte code offset #217
/*     */     //   Java source line #334	-> byte code offset #224
/*     */     //   Java source line #335	-> byte code offset #231
/*     */     //   Java source line #336	-> byte code offset #237
/*     */     //   Java source line #337	-> byte code offset #251
/*     */     //   Java source line #326	-> byte code offset #254
/*     */     //   Java source line #327	-> byte code offset #256
/*     */     //   Java source line #328	-> byte code offset #263
/*     */     //   Java source line #329	-> byte code offset #269
/*     */     //   Java source line #331	-> byte code offset #272
/*     */     //   Java source line #332	-> byte code offset #293
/*     */     //   Java source line #333	-> byte code offset #299
/*     */     //   Java source line #334	-> byte code offset #306
/*     */     //   Java source line #335	-> byte code offset #313
/*     */     //   Java source line #336	-> byte code offset #319
/*     */     //   Java source line #338	-> byte code offset #336
/*     */     // Local variable table:
/*     */     //   start	length	slot	name	signature
/*     */     //   0	337	0	ds	DataSource
/*     */     //   0	337	1	folderIds	List<Integer>
/*     */     //   0	337	2	fullSync	boolean
/*     */     //   15	16	3	status	com.zimbra.common.account.ZAttrProvisioning.AccountStatus
/*     */     //   76	238	4	importStatus	ImportStatus
/*     */     //   81	56	5	Ljava/lang/Object;	Object
/*     */     //   143	159	5	success	boolean
/*     */     //   134	6	6	localObject1	Object
/*     */     //   146	163	6	error	String
/*     */     //   254	16	7	x	ServiceException
/*     */     //   243	6	8	localObject2	Object
/*     */     //   272	62	9	localObject3	Object
/*     */     //   325	6	11	localObject4	Object
/*     */     // Exception table:
/*     */     //   from	to	target	type
/*     */     //   84	103	134	finally
/*     */     //   104	131	134	finally
/*     */     //   134	139	134	finally
/*     */     //   217	240	243	finally
/*     */     //   243	248	243	finally
/*     */     //   152	192	254	com/zimbra/common/service/ServiceException
/*     */     //   152	192	272	finally
/*     */     //   254	274	272	finally
/*     */     //   299	322	325	finally
/*     */     //   325	330	325	finally
/*     */   }
/*     */   
/*     */   public static void resetErrorStatus(DataSource ds)
/*     */   {
/* 341 */     if ((ds.getAttr("zimbraDataSourceFailingSince") != null) || (ds.getAttr("zimbraDataSourceLastError") != null))
/*     */     {
/* 343 */       Map<String, Object> attrs = new HashMap();
/* 344 */       attrs.put("zimbraDataSourceFailingSince", null);
/* 345 */       attrs.put("zimbraDataSourceLastError", null);
/*     */       try {
/* 347 */         Provisioning.getInstance().modifyAttrs(ds, attrs);
/*     */       } catch (ServiceException e) {
/* 349 */         ZimbraLog.datasource.warn("Unable to reset error status for data source %s.", new Object[] { ds.getName() });
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private static void setErrorStatus(DataSource ds, String error) {
/* 355 */     Map<String, Object> attrs = new HashMap();
/* 356 */     attrs.put("zimbraDataSourceLastError", error);
/* 357 */     if (ds.getAttr("zimbraDataSourceFailingSince") == null) {
/* 358 */       attrs.put("zimbraDataSourceFailingSince", DateUtil.toGeneralizedTime(new Date()));
/*     */     }
/*     */     try {
/* 361 */       Provisioning.getInstance().modifyDataSource(ds.getAccount(), ds.getId(), attrs);
/*     */     } catch (ServiceException e) {
/* 363 */       ZimbraLog.datasource.warn("Unable to set error status for data source %s.", new Object[] { ds.getName() });
/*     */     }
/*     */   }
/*     */   
/*     */   private static String generateErrorMessage(Throwable t) {
/* 368 */     StringBuilder buf = new StringBuilder();
/* 369 */     while (t != null)
/*     */     {
/* 371 */       if (t.getClass().getName().startsWith("javax.mail.")) {
/* 372 */         String msg = t.getMessage();
/* 373 */         return msg != null ? msg : t.toString();
/*     */       }
/* 375 */       if (buf.length() > 0) {
/* 376 */         buf.append(", ");
/*     */       }
/* 378 */       String msg = t.getMessage();
/* 379 */       buf.append(msg != null ? msg : t.toString());
/* 380 */       t = t.getCause();
/*     */     }
/* 382 */     return buf.toString();
/*     */   }
/*     */   
/*     */   static void cancelTask(Mailbox mbox, String dsId) throws ServiceException
/*     */   {
/* 387 */     ScheduledTaskManager.cancel(DataSourceTask.class.getName(), dsId, mbox.getId(), false);
/* 388 */     DbScheduledTask.deleteTask(DataSourceTask.class.getName(), dsId);
/*     */   }
/*     */   
/*     */   public static DataSourceTask getTask(Mailbox mbox, String dsId) {
/* 392 */     return (DataSourceTask)ScheduledTaskManager.getTask(DataSourceTask.class.getName(), dsId, mbox.getId());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void cancelSchedule(Account account, String dsId)
/*     */     throws ServiceException
/*     */   {
/* 404 */     updateSchedule(account, null, dsId, true);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void updateSchedule(Account account, DataSource ds)
/*     */     throws ServiceException
/*     */   {
/* 416 */     updateSchedule(account, ds, ds.getId(), false);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static void updateSchedule(Account account, DataSource ds, String dsId, boolean cancelSchedule)
/*     */     throws ServiceException
/*     */   {
/* 432 */     if (!LC.data_source_scheduling_enabled.booleanValue()) {
/* 433 */       return;
/*     */     }
/* 435 */     String accountId = account.getId();
/* 436 */     ZimbraLog.datasource.debug("Updating schedule for account %s, data source %s", new Object[] { accountId, dsId });
/*     */     
/* 438 */     int mboxId = MailboxManager.getInstance().lookupMailboxId(account.getId());
/* 439 */     if (mboxId == -1) {
/* 440 */       return;
/*     */     }
/* 442 */     if (cancelSchedule) {
/* 443 */       ZimbraLog.datasource.info("Data source %s was deleted.  Deleting scheduled task.", new Object[] { dsId });
/*     */       
/* 445 */       ScheduledTaskManager.cancel(DataSourceTask.class.getName(), dsId, mboxId, false);
/* 446 */       DbScheduledTask.deleteTask(DataSourceTask.class.getName(), dsId);
/* 447 */       deleteManaged(accountId, dsId);
/* 448 */       return;
/*     */     }
/* 450 */     if (!ds.isEnabled()) {
/* 451 */       ZimbraLog.datasource.info("Data source %s is disabled.  Deleting scheduled task.", new Object[] { dsId });
/*     */       
/* 453 */       ScheduledTaskManager.cancel(DataSourceTask.class.getName(), dsId, mboxId, false);
/* 454 */       DbScheduledTask.deleteTask(DataSourceTask.class.getName(), dsId);
/* 455 */       return;
/*     */     }
/*     */     
/* 458 */     ZimbraLog.datasource.info("Updating schedule for data source %s", new Object[] { ds.getName() });
/* 459 */     DbPool.DbConnection conn = null;
/*     */     try {
/* 461 */       conn = DbPool.getConnection();
/* 462 */       ScheduledTaskManager.cancel(conn, DataSourceTask.class.getName(), ds.getId(), mboxId, false);
/* 463 */       if (ds.isScheduled()) {
/* 464 */         DataSourceTask task = new DataSourceTask(mboxId, accountId, dsId, ds.getPollingInterval());
/* 465 */         ZimbraLog.datasource.debug("Scheduling %s", new Object[] { task });
/* 466 */         ScheduledTaskManager.schedule(conn, task);
/*     */       }
/* 468 */       conn.commit();
/*     */     } catch (ServiceException e) {
/* 470 */       ZimbraLog.datasource.warn("Unable to schedule data source %s", ds.getName(), e);
/* 471 */       DbPool.quietRollback(conn);
/*     */     } finally {
/* 473 */       DbPool.quietClose(conn);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/datasource/DataSourceManager.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */